﻿namespace PlugNT.Pack.Imager
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;

    public class VerifyCoder
    {
        public HttpContext context;
        public static string SessionName = "Code";

        public VerifyCoder()
        {
            this.context = null;
        }

        public VerifyCoder(HttpContext _context)
        {
            this.context = null;
            this.context = _context;
        }

        private void CreateCheckCodeImage(string checkCode, int fontSize, decimal simpleWidth, int height)
        {
            if ((checkCode != null) && !(checkCode.Trim() == string.Empty))
            {
                Bitmap image = new Bitmap((int) Math.Ceiling((decimal) (checkCode.Length * simpleWidth)), height);
                Graphics g = Graphics.FromImage(image);
                try
                {
                    int i;
                    Random random = new Random();
                    g.Clear(Color.White);
                    for (i = 0; i < 0x19; i++)
                    {
                        int x1 = random.Next(image.Width);
                        int x2 = random.Next(image.Width);
                        int y1 = random.Next(image.Height);
                        int y2 = random.Next(image.Height);
                        g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                    }
                    Font font = new Font("Arial", (float) fontSize, FontStyle.Italic | FontStyle.Bold);
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(2, 10, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                    g.DrawString(checkCode, font, brush, (float) 2f, (float) 2f);
                    for (i = 0; i < 100; i++)
                    {
                        int x = random.Next(image.Width);
                        int y = random.Next(image.Height);
                        image.SetPixel(x, y, Color.FromArgb(random.Next()));
                    }
                    g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                    MemoryStream ms = new MemoryStream();
                    image.Save(ms, ImageFormat.Gif);
                    this.context.Response.ClearContent();
                    this.context.Response.ContentType = "image/Gif";
                    this.context.Response.BinaryWrite(ms.ToArray());
                }
                finally
                {
                    g.Dispose();
                    image.Dispose();
                }
            }
        }

        private string GenerateCheckCode(int strLength)
        {
            string checkCode = string.Empty;
            Random random = new Random();
            for (int i = 0; i < strLength; i++)
            {
                char code;
                int number = random.Next();
                if ((number % 2) == 0)
                {
                    code = (char) (0x30 + ((ushort) (number % 10)));
                }
                else
                {
                    code = (char) (0x41 + ((ushort) (number % 0x1a)));
                }
                checkCode = checkCode + code.ToString();
            }
            if (this.context.Session[SessionName] != null)
            {
                this.context.Session[SessionName] = checkCode;
                return checkCode;
            }
            this.context.Session.Add(SessionName, checkCode);
            return checkCode;
        }

        public string Get()
        {
            object obj = this.context.Session[SessionName];
            return ((obj == null) ? "" : obj.ToString());
        }

        public static string Help()
        {
            return "mailto:354990393@qq.com";
        }

        public void Write()
        {
            this.CreateCheckCodeImage(this.GenerateCheckCode(5), 0x16, Convert.ToDecimal((double) 20.5), 0x23);
        }

        public void Write(int strLength, int fontSize, int simpleWidth, int height)
        {
            this.CreateCheckCodeImage(this.GenerateCheckCode(strLength), fontSize, Convert.ToDecimal(simpleWidth), height);
        }
    }
}

